package com.hongdee.crm.tdc.controller;

import com.github.pagehelper.PageInfo;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.crm.tdc.pojo.MarketQuestionLibraryPojo;
import com.hongdee.crm.tdc.service.IMarketQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangkai
 * @desc 一句话描述
 **/
@RequestMapping("/crm/tdc/question")
@RestController
public class MarketQuestionController {

    @Autowired
    IMarketQuestionService questionService;

    /**
     * 营销预计-三日回访-调研问券-分页查询子列表
     *
     * @param uid
     * @param token
     * @param pId
     * @param page
     * @param limit
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "pId", defaultValue = "") String pId,
                    @RequestParam(name = "brandCode", defaultValue = "") String brandCode,
                    @RequestParam(name = "page", defaultValue = "0") int page,
                    @RequestParam(name = "limit", defaultValue = "10") int limit
    ) throws Exception {

        PageInfo info = questionService.page(page, limit, pId, brandCode);
        Map map = new HashMap(10);
        map.put("total", info.getTotal());
        map.put("dataList", info.getList());
        map.put("success", true);
        map.put("msg", "查询成功");
        return map;
    }


    /**
     * 营销预计-三日回访-调研问券-问券树
     *
     * @param uid
     * @param token
     * @param pId
     * @param page
     * @param limit
     * @return
     * @throws Exception
     */
    @RequestMapping("/tree")
    @ResponseBody
    public Map tree(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "pId", defaultValue = "") String pId,
                    @RequestParam(name = "brandCode", defaultValue = "") String brandCode,
                    @RequestParam(name = "page", defaultValue = "0") int page,
                    @RequestParam(name = "limit", defaultValue = "10") int limit
    ) throws Exception {

        List<MarketQuestionLibraryPojo> ls = questionService.treeLibaray(pId);
        Map map = new HashMap(10);
        map.put("total", ls.size());
        map.put("dataList", ls);
        map.put("success", true);
        map.put("msg", "查询成功");
        return map;
    }


    /**
     * 营销预计-三日回访-3DC-问券树
     *
     * @param uid
     * @param token
     * @param companyId
     * @param preYear
     * @return
     * @throws Exception
     */
    @RequestMapping("save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "companyId", defaultValue = "") String companyId,
                             @RequestParam(name = "preYear", defaultValue = "") String preYear,
                             @RequestBody MarketQuestionLibraryPojo po
    ) throws Exception {
        companyId = TokenUtil.getCompanyId(uid);
        questionService.saveOrUpdateLibary(po);
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setMsg("更新成功！");
        resultEntity.setSuccess(true);
        return resultEntity;
    }

    /**
     * 营销预计-三日回访-3DC-标签详情
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id
    ) throws Exception {
        MarketQuestionLibraryPojo po =  questionService.info(id);
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setMsg("更新成功！");
        resultEntity.setData(po);
        resultEntity.setSuccess(true);
        return resultEntity;
    }
    /**
     * 营销预计-三日回访-3DC-更新问题状态
     *
     * @param uid
     * @param token
     * @param id
     * @param status
     * @return
     * @throws Exception
     */
        @RequestMapping("updateStatus")
    @ResponseBody
    public ResultEntity updateStatus(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestParam(name = "id", defaultValue = "") String id,
                                     @RequestParam(name = "status", defaultValue = "") String status
    ) throws Exception {

        questionService.updateLibaryStatus(id, status);
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setMsg("更新成功！");
        resultEntity.setSuccess(true);
        return resultEntity;
    }


    /**
     * 营销预计-三日回访-调研问券-问券树
     *
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/fullTree")
    @ResponseBody
    public Map fullTree(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {

        List<MarketQuestionLibraryPojo> ls = questionService.fullTreeLibaray();
        Map map = new HashMap(10);
        map.put("total", ls.size());
        map.put("dataList", ls);
        map.put("success", true);
        map.put("msg", "查询成功");
        return map;
    }

}
